package com.pactera.madp.cp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pactera.madp.cp.api.dto.track.BehaviorTrackDto;
import com.pactera.madp.cp.api.entity.*;
import com.pactera.madp.cp.api.entity.clientLossManagement.RemindDeleteEntity;
import com.pactera.madp.cp.common.constant.BehaviorTrackFormatConst;
import com.pactera.madp.cp.common.constant.Constant;
import com.pactera.madp.cp.common.constant.WxApiConst;
import com.pactera.madp.cp.common.util.WxApiUtils;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.mapper.WorkContactEmployeeMapper;
import com.pactera.madp.cp.service.IWorkContactEmployeeService;
import com.pactera.madp.cp.service.IWorkContactService;
import com.pactera.madp.cp.service.IWorkEmployeeService;
import com.pactera.madp.cp.service.RemindDeleteService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Slf4j
@Service
@AllArgsConstructor
public class BehaviorTrackLostServiceImpl extends BaseBehaviorTrackServer {

    private final RemindDeleteService remindDeleteService;

    private final WorkContactEmployeeMapper contactEmployeeMapper;

    @Override
    public void handleEvent(BehaviorTrackDto dto, IWorkEmployeeService workEmployeeService,
                            IWorkContactService workContactService, HttpServletResponse response) throws Exception{
        //是否提醒
        List<RemindDeleteEntity> remindList = remindDeleteService.list();
        if(remindList.isEmpty()) {
            return;
        }
        if(remindList.get(0).getStatus() == 0) {
            //提醒关闭
            return;
        }

        //事件处理
        BehaviorTrackEntity entity = new BehaviorTrackEntity();
        BeanUtil.copyProperties(dto, entity);
        baseHandleEvent(entity, workEmployeeService, workContactService, this, response);
    }

    @Override
    protected String buildData(BehaviorTrackEntity entity) {
        List<WorkContactEmployeeEntity> contactEmployeeEntities = contactEmployeeMapper.getLossContact(entity.getCorpId(), entity.getContactId(), entity.getEmployeeId());
        //根据更新时间取该客户与成员关系最新的记录
        WorkContactEmployeeEntity max = contactEmployeeEntities.stream().max(Comparator.comparing(l -> l.getUpdateTime())).get();
        List<String> datas = new ArrayList<>();
        datas.add(entity.getUsername());
        datas.add(entity.getExternalUsername());
        if (!JSONArray.parseArray(max.getRemarkMobiles()).isEmpty()) {
            datas.add(JSONArray.parseArray(max.getRemarkMobiles()).get(0).toString());
        }
        datas.add(entity.getWxUserId());
        datas.add(entity.getWxExternalUserid());
        String content = "";
        if (!JSONArray.parseArray(max.getRemarkMobiles()).isEmpty()) {
            content = String.format(BehaviorTrackFormatConst.LostFormat1, datas.toArray(new String[datas.size()]));
        } else {
            content = String.format(BehaviorTrackFormatConst.LostFormat2, datas.toArray(new String[datas.size()]));
        }

        return content;
    }

}